package com.larissa.quiz;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavDirections;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;

import java.util.List;
import java.util.UUID;

public class SingleQuestionFragment extends Fragment {
    private static final String TAG="SingleQuestionFragment";
    private Question question;
    private EditText questionText;
    private CheckBox ckbTrue;
    private CheckBox ckbFalse;
    private Button btnSave;
    private QuestionViewModel viewmodel;

    @Override
    public void onAttach(Context context){
        super.onAttach(getContext());
        Log.d(TAG,"onAttach() called");
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Log.d(TAG,"onCreate() called");
        UUID id=SingleQuestionFragmentArgs.fromBundle(getArguments()).getId();
        viewmodel=new ViewModelProvider(requireActivity()).get(QuestionViewModel.class);
        viewmodel.loadQuestion(id);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        super.onCreateView(inflater,container,savedInstanceState);
        Log.d(TAG,"onCreateView() called");
        View view=inflater.inflate(R.layout.fragment_single_question,container,false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        questionText=view.findViewById(R.id.question_title);
        ckbTrue=view.findViewById(R.id.ckb_true);
        ckbFalse=view.findViewById(R.id.ckb_false);
        btnSave=view.findViewById(R.id.btn_save_question);

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateQuestion();
            }
        });

        viewmodel.questionLiveData.observe(getViewLifecycleOwner(), new Observer<Question>() {
            @Override
            public void onChanged(Question question1) {
                question=question1;
                questionText.setText(question.getQuestionText());
                if(question.isQuestionAnswer())
                    ckbTrue.setChecked(true);
                else
                    ckbFalse.setChecked(true);
            }
        });

    }

    private void updateQuestion(){
        String text=questionText.getText().toString();
        Boolean answer=false;
        if(ckbTrue.isChecked())
            answer=true;
        question.setQuestionText(text);
        question.setQuestionAnswer(answer);
        viewmodel.updateQuestion(question);
        NavHostFragment.findNavController(this).popBackStack();
    }

    @Override
    public void onDestroyView(){
        super.onDestroyView();
        Log.d(TAG,"onDestroyView() called");
    }
    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.d(TAG,"onDestroy() called");
    }

    @Override
    public void onDetach(){
        super.onDetach();
        Log.d(TAG,"onDetach() called");
    }
}
